#!/bin/bash

function os_sys()
{
    #系统信息
    os_type=$(uname)
    os_version=`hostnamectl | grep "Operating System" | awk '{print $3,$4,$5}'`
    os_ker=$(uname -r)
    os_time=$(date +%F_%T)
    os_last_reboot=$(uptime |awk '{print $3,$4}'| awk -F ',' '{print $1}')
    os_hostname=$(hostname)


    echo "#操作系统的类型：" "${os_type}"
    echo "#操作系统版本：" "${os_version}"
    echo "#操作系统的内核版本：" "${os_ker}"
    echo "#服务器当前的运行时间：" "${os_time}"
    echo "#服务器最后重启时间：" "${os_last_reboot}"
    echo "#服务器的主机名：" "${os_hostname}"
}


network_card=`ip -4 addr | grep "scope global dynamic" | awk '{print $10}'`
function os_network()
{
#网络信息
    ip_addr=$(ifconfig $network_card |awk '/netmask/{print $2}')
    echo "服务器的IP地址为：${ip_addr}"
    ping -c2 www.baidu.com > /dev/null
    if [ $? -eq 0 ];then
	    echo "服务器的网络正常"
    else
	    echo "数据异常请检查网路"
    fi
    network_card=`ip -4 addr | grep "scope global dynamic" | awk '{print $10}'`
    RX=$(ifconfig $network_card | grep RX | sed -n '1p' | awk '{print $(NF-1),$NF}')
    TX=$(ifconfig $network_card | grep TX | sed -n '1p' | awk '{print $(NF-1),$NF}')

    echo "网卡流入的量为：${RX}"
    echo "网卡流出的量为：${TX}"
    echo " "

}

function cpu_info()
{
    #cpu
    cpu_num=$(cat /proc/cpuinfo |grep "physical id" | sort |uniq |wc -l)
    cpu_core=$(cat /proc/cpuinfo |grep "core id" |sort |uniq |wc -l)
    cpu_model=$(cat /proc/cpuinfo |grep "model name" |uniq |awk '{print $4,$6,$7,$9}')
    
    echo "cpu的物理个数为：${cpu_num}"
    echo "cpu的核心个数为：${cpu_core}"
    echo "cpu的型号：${cpu_model}"
    echo " "
}

#mem
function mem_info()
{
    #内存总大小
    mem_total=$(free -m |awk '/Mem/{print $2}')
    #已使用内存
    mem_used=$(free -m |awk '/Mem/{print $3}')
    #可使用内存大小
    mem_free=$(free -m |awk '/Mem/{print $7}')
    #已使用内存百分比
    p_bfb=$(free -m |awk '/Mem/{printf "%.2f\n",$3/$2*100}')
    #可使用内存百分比 
    s_bfb=$(free -m |awk '/Mem/{printf "%.2f\n",$7/$2*100}')
    

    echo "内存总大小: ${mem_total}"
    echo "已用内存大小：${mem_used}"
    echo "剩余内存大小：${mem_free}"
    echo "已使用内存百分比：${p_bfb}%"
    echo "剩余内存百分比：${s_bfb}%"
    echo " "

}

function disk_info()
{
    #disk
    #磁盘总量
    disk_total=$(lsblk |awk '/disk/{print $4}')
    count=($(df -Tm |egrep -v tmpfs |sed '1d' |awk '{print $5}'))
    sum=0
    for i in ${count[@]}
    do
    let sum=sum+"${i}"
    done
    sumb=("${sum}"/1024)
    
    echo " 磁盘总量为：${disk_total}"
    echo "剩余磁盘总量为 $sum M"

}

function main()
{
	os_sys
	os_network
	cpu_info
	mem_info		
	disk_info
}

    #while :
    #do
        main
        #slepp 5
    #done
    

